from django.db import models
from utils.models import BaseModel


#     -实战课表：
#     -课程分类表（跟课程一对多，一个分类对应多门课程）
#     -老师表（跟课程是一对多，一个老师对应多门课程）
#     -章节表 （跟课程一对多，一个课程对应多个章节）
#     -课时表（跟章节是一对多，一个章节，多个课时）


# 课程分类表
class CourseCategory(BaseModel):
    name = models.CharField(max_length=64, unique=True, verbose_name='分类名称')

    class Meta:
        db_table = 'luffy_course_category'
        verbose_name_plural = '分类'
    def __str__(self):
        return self.name

# 实战课表
class Course(BaseModel):
    # 课程类型
    course_type = (
        (1,'付费'),
        (2,'VIP专享'),
        (3,'学位课程'),
    )
    # 等级
    level_choice = (
        (1,'初级'),
        (2,'中级'),
        (3,'高级'),
    )
    # 状态
    status_choice = (
        (1,'上线'),
        (2,'下线'),
        (3,'预上线'),
    )
    name = models.CharField(max_length=128,verbose_name='课程名称')
    course_img = models.ImageField(upload_to='courses',max_length=255,verbose_name='封面图片',blank=True,null=True)
    course_type = models.SmallIntegerField(choices=course_type,default=0,verbose_name='付费类型')
    # 课程详情描述,存html内容
    brief = models.TextField(max_length=2048,verbose_name='详情介绍',null=True,blank=True)
    level = models.SmallIntegerField(choices=level_choice,default=0,verbose_name='难度等级')
    pub_date = models.DateField(verbose_name='发布日期',auto_now_add=True)
    period = models.IntegerField(verbose_name='建议学习周期(day)',default=7)
    attachment_path = models.FileField(upload_to='attachment',max_length=128,verbose_name='课件路径',blank=True,null=True)
    status = models.SmallIntegerField(choices=status_choice,default=0,verbose_name='课程状态')
    students = models.IntegerField(verbose_name='学习人数',default=0)
    sections = models.IntegerField(verbose_name='总课时数量',default=0)
    pub_sections = models.IntegerField(verbose_name='课时更新数量',default=0)
    price = models.DecimalField(max_digits=6,decimal_places=2,verbose_name='课程原价',default=0)
    teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="授课老师")
    course_category = models.ForeignKey("CourseCategory", on_delete=models.SET_NULL, db_constraint=False, null=True,
                                        blank=True,
                                        verbose_name="课程分类")
    class Meta:
        db_table = 'luffy_course'
        verbose_name_plural = '课程'
    def __str__(self):
        return self.name
    # 方法二：
    # def teacher_detail(self):
    #     return {'name':self.teacher.name}

    @property
    def course_section_list(self):
        # 根据课程拿到所有章节，从章节中一个个拿出课时，放到一个列表中，在循环的过程中判断列表长度是不是大于4，如果大于4，直接返回
        course_se_list = []
        # 根据课程拿到所有章节
        course_chapter_list = self.coursechapters.all()
        for course_chapter in course_chapter_list:
            # 通过这个章节，获取所有课时
            section_list = course_chapter.coursesections.all()
            for section in section_list:
                course_se_list.append({'id': section.id, 'name': section.name, 'free_trail': section.free_trail,
                                       'section_link': section.section_link})

                if len(course_se_list) >= 4:
                    return course_se_list

        return course_se_list

    @property
    def level_name(self):
        return self.get_level_display()
# 老师表
class Teacher(BaseModel):
    role_choice = (
        (0,'讲师'),
        (1,'导师'),
        (2,'班主任'),
    )
    name = models.CharField(max_length=32,verbose_name='导师名')
    role = models.SmallIntegerField(choices=role_choice,default=0,verbose_name='导师身份')
    title = models.CharField(max_length=64,verbose_name='职位')
    signature = models.CharField(max_length=255,verbose_name='导师签名',help_text='导师签名',blank=True,null=True)
    image = models.ImageField(upload_to='teacher',null=True,verbose_name='导师封面')
    brief = models.TextField(max_length=1024,verbose_name='导师描述')
    class Meta:
        db_table = 'luffy_teacher'
        verbose_name_plural = '导师'
    def __str__(self):
        return self.name

    def role_name(self):
        # 当前对象.get_字段名_display()---->拿到choice数字对应的中文
        return self.get_role_display()


# 章节表
class CourseChapter(BaseModel):
    chapter = models.SmallIntegerField(verbose_name='第几章',default=1)
    name = models.CharField(max_length=128,verbose_name='章节标题')
    summary = models.TextField(verbose_name='章节介绍',blank=True,null=True)
    pub_date = models.DateField(verbose_name='发布日期',auto_now_add=True)
    course = models.ForeignKey('course',related_name='coursechapters',on_delete=models.CASCADE,verbose_name='课程名称')
    class Meta:
        db_table = 'luffy_course_chapter'
        verbose_name_plural = '章节'
    def __str__(self):
        return "%s:(第%s章)%s" % (self.course, self.chapter, self.name)

# 课时表
class CourseSection(BaseModel):
    section_type_choices = (
        (0,'文档'),
        (1,'练习'),
        (2,'视频'),
    )
    name = models.CharField(max_length=128,verbose_name='课时标题')
    orders = models.PositiveSmallIntegerField(verbose_name='课时排序')
    section_type = models.SmallIntegerField(default=2,choices=section_type_choices,verbose_name='课时种类')
    section_link = models.CharField(max_length=255,blank=True,null=True,verbose_name='课时链接',
                                    help_text='若是video,填vid,若是文档,填link')
    duration = models.CharField(verbose_name='视频时长',blank=True,null=True,max_length=32)
    pub_data = models.DateTimeField(verbose_name='发布时间',auto_now_add=True)
    free_trail = models.BooleanField(verbose_name='免费试看',default=False)
    chapter = models.ForeignKey(to='CourseChapter',related_name='coursesections',on_delete=models.CASCADE,
                                verbose_name='课程章节')
    class Meta:
        db_table = 'luffy_course_section'
        verbose_name_plural = '课时'
    def __str__(self):
        return "%s-%s" % (self.chapter, self.name)

    def section_type_name(self):
        return self.get_section_type_display()